package springboot.study.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springboot.study.project.common.Result;
import springboot.study.project.model.Clazz;
import springboot.study.project.model.Department;
import springboot.study.project.model.Major;
import springboot.study.project.service.CommonService;
import springboot.study.project.util.common.ResultUtil;
import springboot.study.project.util.nCoV_Crawler2019.EpidemicInfoService;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: CommonController
 * @Description: 通用控制器
 * @Author: liu-hao
 * @Date: 2021-04-11 15:49
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    CommonService commonService;

    /**
     * 获取疫情数据
     *
     * @return
     */
    @RequestMapping("/getEpidemicData")
    Result getEpidemicData() {
        Map<String, Integer> epidemicInfo = EpidemicInfoService.epidemicInfo;
        if (epidemicInfo == null || epidemicInfo.size() <= 0) {
            return ResultUtil.failure("未查询到疫情信息！");
        }
        return ResultUtil.success(epidemicInfo);
    }

    @RequestMapping("/getAllDept")
    Result getAllDept() {
        List<Department> allDept = commonService.getAllDept();
        if (allDept == null || allDept.size() <= 0) {
            return ResultUtil.failure("未查询到院系！");
        }
        return ResultUtil.success(allDept);
    }

    @RequestMapping("/getMajorByDeptId")
    Result getMajorByDeptId(String deptId) {
        List<Major> majorList = commonService.getMajorByDeptId(deptId);
        if (majorList == null || majorList.size() <= 0) {
            return ResultUtil.failure("未查询到专业！");
        }
        return ResultUtil.success(majorList);
    }

    @RequestMapping("/getClazzByMajorId")
    Result getClazzByMajorId(String majorId) {
        List<Clazz> clazzList = commonService.getClazzByMajorId(majorId);
        if (clazzList == null || clazzList.size() <= 0) {
            return ResultUtil.failure("未查询到班级！");
        }
        return ResultUtil.success(clazzList);
    }

    // 院系相关

    @RequestMapping("/addDept")
    public Result addDept(@RequestBody Department department) {
        boolean result = commonService.addDept(department);
        if (!result) {
            return ResultUtil.failure("添加院系失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/removeDept")
    public Result removeDept(@RequestParam(name = "deptId") String deptId) {
        boolean result = commonService.removeDept(deptId);
        if (!result) {
            return ResultUtil.failure("删除院系失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/modifyDept")
    public Result modifyDept(@RequestBody Department department) {
        boolean result = commonService.modifyDept(department);
        if (!result) {
            return ResultUtil.failure("添加院系失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/findDeptInfo")
    public Map<String, Object> findDeptInfo(@RequestParam(name = "deptName") String deptName,
                                            @RequestParam(name = "page", defaultValue = "1") int pageNum,
                                            @RequestParam(name = "limit", defaultValue = "10") int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        Map<String, Object> deptInfoMap = commonService.findDeptInfo(deptName, offset, pageSize);
        return deptInfoMap;
    }

    // 专业相关
    @RequestMapping("/addMajor")
    public Result addMajor(@RequestBody Major major) {
        boolean result = commonService.addMajor(major);
        if (!result) {
            return ResultUtil.failure("添加专业失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/removeMajor")
    public Result removeMajor(@RequestParam(name = "majorId") String majorId) {
        boolean result = commonService.removeMajor(majorId);
        if (!result) {
            return ResultUtil.failure("删除专业失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/modifyMajor")
    public Result modifyMajor(@RequestBody Major major) {
        boolean result = commonService.modifyMajor(major);
        if (!result) {
            return ResultUtil.failure("添加专业失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/findMajorInfo")
    public Map<String, Object> findMajorInfo(@RequestParam(name = "majorName") String majorName,
                                             @RequestParam(name = "page", defaultValue = "1") int pageNum,
                                             @RequestParam(name = "limit", defaultValue = "10") int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        Map<String, Object> majorInfoMap = commonService.findMajorInfo(majorName, offset, pageSize);
        return majorInfoMap;
    }

    // 班级相关

    @RequestMapping("/addClazz")
    public Result addClazz(@RequestBody Clazz clazz) {
        boolean result = commonService.addClazz(clazz);
        if (!result) {
            return ResultUtil.failure("添加班级失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/removeClazz")
    public Result removeClazz(@RequestParam(name = "clazzId") String clazzId) {
        boolean result = commonService.removeClazz(clazzId);
        if (!result) {
            return ResultUtil.failure("删除班级失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/modifyClazz")
    public Result modifyClazz(@RequestBody Clazz classes) {
        boolean result = commonService.modifyClazz(classes);
        if (!result) {
            return ResultUtil.failure("添加班级失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/findClazzInfo")
    public Map<String, Object> findClazzInfo(@RequestParam(name = "clazzName") String clazzName,
                                             @RequestParam(name = "page", defaultValue = "1") int pageNum,
                                             @RequestParam(name = "limit", defaultValue = "10") int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        Map<String, Object> clazzInfoMap = commonService.findClazzInfo(clazzName, offset, pageSize);
        return clazzInfoMap;
    }
}
